package com.zzyq.pc.web.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzyq.pc.common.plugin.db.PageView;
import com.zzyq.pc.utils.StringUtil;
import com.zzyq.pc.web.submit.model.Submit;
import com.zzyq.pc.web.submit.service.ISubmitService;
import com.zzyq.pc.web.user.mapper.PermissionMapper;
import com.zzyq.pc.web.user.mapper.RoleMapper;
import com.zzyq.pc.web.user.mapper.UserMapper;
import com.zzyq.pc.web.user.model.Permission;
import com.zzyq.pc.web.user.model.Role;
import com.zzyq.pc.web.user.model.User;
import com.zzyq.pc.web.user.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    ISubmitService submitService;

    @Override
    public boolean addUser(User user) {
        // 插入用户并返回主键
        int result = userMapper.insert(user);
        if (result > 0) {
            // 将用户和角色关联
            for (Role role : user.getRoles()) {
                Role role2 = roleMapper.selectByEnum(role.getRoleEnum());
                roleMapper.insertUserRole(user.getUserId(), role2.getRoleId());
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean updateUser(User user) {
        if (user == null || user.getUserId() == null) {
            return false;
        }
        int result = userMapper.update(user);
        if (result > 0) {
            // 修改用户角色
            for (Role role : user.getRoles()) {
                // 先删除再新增
                roleMapper.deleteByUserId(user.getUserId());
                Role role2 = roleMapper.selectByEnum(role.getRoleEnum());
                roleMapper.insertUserRole(user.getUserId(), role2.getRoleId());
            }
            return true;
        }
        return false;
    }

    @Override
    public List<Role> findRoles(Integer userId) {
        return roleMapper.selectRolesByUserId(userId);
    }

    @Override
    public List<Permission> findPermissions(Integer userId) {
        return permissionMapper.selectPermissionsByUserId(userId);
    }

    @Override
    public User findUserByAccount(String account) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtil.isPhoneLegal(account)) {
            map.put("phone", account);
        } else {
            map.put("account", account);
        }
        return userMapper.selectUserByAccount(map);
    }

    @Override
    public List<User> selectSameList(User user) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", user.getUserPhone());
        map.put("account", user.getUserAccount());
        map.put("userId", user.getUserId());
        return userMapper.selectSameList(map);
    }

    @Override
    public boolean deleteByDepartId(Integer departId) {
        if (departId == null) {
            return false;
        }
        return userMapper.deleteByDepartId(departId) > 0;
    }

    @Override
    public List<User> selectByDepartId(Integer departId) {
        if (departId == null) {
            return null;
        }
        return userMapper.selectByDepartId(departId);
    }

    @Override
    public List<User> selectAsList(PageView pageView, User user) {
        Map<String, Object> map = new HashMap<>();
        if (pageView != null) {
            map.put("paging", pageView);
        }
        map.put("userId", user.getUserId());
        map.put("userAccount", user.getUserAccount());
        map.put("userPhone", user.getUserPhone());
        map.put("userName", user.getUserName());
        map.put("companyId", user.getCompany().getCompanyId());
        map.put("departId", user.getDepart().getDepartId());
        map.put("roleGrade", user.getMaxRole().getRoleGrade());
        return userMapper.selectAsList(map);
    }

    @Override
    public User selectUserById(Integer userId) {
        if (userId == null) {
            return null;
        }
        return userMapper.selectUserById(userId);
    }

    @Override
    public boolean updatePwd(Integer userId, String pwd) {
        if (userId == null) {
            return false;
        }
        return userMapper.updatePwd(userId, pwd) > 0;
    }

    @Override
    public boolean deleteById(Integer userId) {
        if (userId == null) {
            return false;
        }
        return userMapper.deleteById(userId) > 0;
    }

    @Override
    public boolean selectTasks(Integer userId) {
        if (userId == null) {
            return false;
        }
        List<Submit> submits = submitService.selectUntreatedByUserId(userId);
        if (submits == null || submits.isEmpty()) {
            return false;
        }
        return true;
    }

    @Override
    public List<User> selectForSubmit(Integer userId, Integer departId, Integer maxGrade, Integer minGrade) {
        if (departId == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("departId", departId);
        map.put("maxGrade", maxGrade);
        map.put("minGrade", minGrade);
        return userMapper.selectForSubmit(map);
    }
}
